package cn.fintecher.pangolin.service.dataimp.task;

import cn.fintecher.pangolin.common.enums.ImportDataExcelStatus;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.elastic.DataImportRecord;
import cn.fintecher.pangolin.entity.elastic.LeaveCaseModel;
import cn.fintecher.pangolin.entity.mysql.CaseInfo;
import cn.fintecher.pangolin.service.dataimp.elastic.DataImportRecordElastic;
import cn.fintecher.pangolin.service.dataimp.elastic.LeaveCaseModelElastic;
import cn.fintecher.pangolin.service.dataimp.repository.CaseInfoRepository;
import cn.fintecher.pangolin.common.utils.RepositoryUtil;
import org.apache.commons.collections4.IterableUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 16:49 2019/11/5
 */

@Service("cancelLeaveTask")
public class CancelLeaveTask {
    private static final Logger log = LoggerFactory.getLogger(CancelLeaveTask.class);

    @Autowired
    private LeaveCaseModelElastic leaveCaseModelElastic;

    @Autowired
    private DataImportRecordElastic dataImportRecordElastic;

    @Autowired
    private ProcessCaseFuture processCaseFuture;

    @Autowired
    private CaseInfoRepository caseInfoRepository;

    /**
     * 撤销留案导入
     */
    @Async
    @Transactional
    public void cancelLeave(BoolQueryBuilder builder, DataImportRecord dataImportRecord, UserModel userModel) {
        //创建任务集合
        List<CompletableFuture<List<CaseInfo>>> list = new ArrayList<>();
        Iterable<LeaveCaseModel> iterable = leaveCaseModelElastic.search(builder);
        List<LeaveCaseModel> leaveCaseModels = IterableUtils.toList(iterable);
        //分页处理信息
        int pageSize = 2000;
        int pageNumber = (int) Math.ceil((double) leaveCaseModels.size() / (double) pageSize);
        for (int i = 0; i < pageNumber; i++) {
            List<LeaveCaseModel> models = leaveCaseModels.stream().skip(i * pageSize).limit(pageSize).collect(Collectors.toList());
            CompletableFuture<List<CaseInfo>> task = processCaseFuture.processCaseInfo(models, null, 0);
            list.add(task);
        }
        try {
            List<CaseInfo> caseInfos = new ArrayList<>();
            for (CompletableFuture<List<CaseInfo>> future : list) {
                caseInfos.addAll(future.get());
            }
            leaveCaseModels.forEach(e -> e.setFlag(1));
            RepositoryUtil.saveData(leaveCaseModelElastic, leaveCaseModels);
            RepositoryUtil.saveData(caseInfoRepository, caseInfos);
            dataImportRecord.setStatus(ImportDataExcelStatus.BILL_REVOKE);
            dataImportRecord.process(userModel.getRealName(), ZWDateUtil.getNowDateTime());
            dataImportRecordElastic.save(dataImportRecord);
        } catch (InterruptedException | ExecutionException e) {
            log.error(e.getMessage(), e);
            dataImportRecord.setStatus(ImportDataExcelStatus.UN_CONFIRMED);
            dataImportRecord.process(userModel.getRealName(), ZWDateUtil.getNowDateTime());
            dataImportRecordElastic.save(dataImportRecord);
        }
    }
}
